1312. Minimum Insertion Steps to Make a String Palindrome 發表於 2023-02-13 | 分類於 leetcode problemsolution123456789101112131415161718192021222324252627class Solution {public: int minInsertions(string s) { // m b a d m // //m 0 1 2 3 2 //b 0 1 2 3 //a 0 1 2 //d 0 1 //m 0 // if(s[i]!=s[j]) dp[i][j] = 1+min(dp[i+1][j], dp[i][j-1]); int n =s.size(); vector<vector<int>> dp(n, vector<int>(n,0)); for(int i=n-2;i>-1;--i){ for(int j = i+1;j<n;++j){ // 形成回文 if(s[i] == s[j]) dp[i][j] = dp[i+1][j-1]; // 插入 else dp[i][j] = 1 + min(dp[i+1][j] ,dp[i][j-1]); } } return dp[0].back(); }}; analysis time complexity O(nm) space complexity O(nm)